package com.devgroupvn.guinness.view;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.devgroupvn.guinness.R;
import com.google.ads.AdRequest;
import com.google.ads.AdView;

public class webviewloaddata extends Activity
{
	WebView mWebview;
	ProgressBar progressbar;
	String url;
	String data;
	AdView adview;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.webviewloaddata);
		mWebview = (WebView) findViewById(R.id.webviewdata);
		progressbar = (ProgressBar) findViewById(R.id.progressbar);
		adview =(AdView)findViewById(R.id.adView);
		initWebview();
		setupAdwhirl();
		Bundle bundle = getIntent().getExtras();
		if(bundle!=null)
		{
			url = bundle.getString("link");
			mWebview.loadUrl(url);
//			new AsynLoaddata().execute();
		}
	}
	
	private void setupAdwhirl()
	{
		try
		{
			final Handler handle = new Handler();
			final Runnable run = new Runnable()
			{

				@Override
				public void run()
				{
					AdRequest request = new AdRequest();
					adview.loadAd(request);
					handle.postDelayed(this, 5000);

				}
			};
			handle.postDelayed(run, 5000);
		}
		catch (Exception e)
		{

		}
	}
	
	private class AsynLoaddata extends AsyncTask<Void, Void, Void>
	{

		@Override
		protected Void doInBackground(Void... params)
		{
//			data = new GetItems().getItemDetail(url);
			return null;
		}

		@Override
		protected void onPostExecute(Void result)
		{
			mWebview.loadData(data, "text/html", "utf-8");
			progressbar.setVisibility(View.GONE);
			super.onPostExecute(result);
		}
		
	}
	
	private void initWebview()
	{
		mWebview.setWebChromeClient(new WebChrome());
		mWebview.setWebViewClient(new Webclient());
    	
		mWebview.getSettings().setJavaScriptEnabled(true);
		mWebview.getSettings().setBuiltInZoomControls(true);
		mWebview.setHorizontalScrollBarEnabled(false);
		mWebview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
		mWebview.setVerticalScrollbarOverlay(true);
	}

	private class WebChrome extends WebChromeClient
	{

		@Override
		public void onProgressChanged(WebView view, int newProgress)
		{
			super.onProgressChanged(view, newProgress);
			{
				  if(newProgress < 100){
                      progressbar.setVisibility(View.VISIBLE);
                  }
                  if(newProgress == 100) {
                	  progressbar.setVisibility(ProgressBar.GONE);
                  }
			}

		}
	}
	private class Webclient extends WebViewClient{
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {

			view.loadUrl(url);
			
			return true;
		}
	}
	
}
